﻿using DeepEarth.Map.Core;
using System.Collections.Generic;
using System;
using System.Linq;
using DeepEarth.Map.Core.Tiling;

namespace DeepEarth.Map.TileSources
{
	public class MapServerTileSourceFactory : BaseTileSourceFactory
	{
        public static readonly MapMode GoogleMap = new MapMode("mapServer", "google", "GoogleMap", "Google Map");
        public static readonly MapMode BingMap = new MapMode("mapServer", "bing", "BingMap", "Bing Map");
        public static readonly MapMode WMS = new MapMode("mapServer", "wms", "WMS", "WMS");

        private static readonly MapMode[] _availableModes = new[] { GoogleMap, BingMap, WMS };

		private readonly MapServerMapTileSource GoogleMapTileSource = new MapServerMapTileSource();

        public override IList<MapMode> AvailableModes { get { return _availableModes; } }

        public static bool IsValidMode(MapMode mode)
		{
			return _availableModes.Contains(mode);
		}

        public static void ValidateMode(MapMode mode)
		{
			if (!IsValidMode(mode))
				throw new ArgumentOutOfRangeException(String.Format("Map mode '{0}:{1}' is not valid for the MapServer provider",
																	mode.ProviderKey,
																	mode.Identifier));
		}

        public override BaseTileSource CreateTileSource(MapMode mode)
		{
			mode = ValidateAndCoerceMapMode(mode);
			if (mode == GoogleMap)
				return GoogleMapTileSource;

			throw new NotImplementedException("The provider for mode '" + mode.Name + "' is not implemented.");
		}
	}
}
